<?php

namespace app\customer\logic;
use app\data\util\HttpUtil;
use think\facade\Db;
class MessageLogic extends BasicLogic
{

    /**
     * @desc   获取发送消息配置列表接口
     * @author luckyXiong
     * @date   2025-01-24
     */
   public function messageList($params=[]){
       $create_time             = getIsset($params,'create_time');
       $shop_id                 = getIsset($params,'shop_id');
       $limit                   = getIsset($params,'limit',10);
       $where = [];
       if ($create_time) {
           $where[]  = ['create_time', '>=', date('Y-m-d H:i:s',strtotime($create_time[0]))];
           $where[]  = ['create_time', '<=', date('Y-m-d H:i:s',strtotime($create_time[1]) + 3600*24-1)];
       }

       if($shop_id){
           $where[]  = ['shop_id', '=',$shop_id];
       }

       $order = ['create_time desc'];
       $res   = Db::name('customer_email_config')
           ->where($where)
           ->order($order)
           ->paginate($limit);
       $list = $res->items()?:[];
       $shop_ids       = array_column($list,'shop_id');
       $shop_names     = Db::name('shops')->field('id,name,myshopify_domain')->whereIn('id',$shop_ids)->select()->toArray();
       $shop_names_map = array_column($shop_names,null,'id');
       foreach($list as $k=>$v){
           $list[$k]['date_zone'] = $v['date_zone_start'].'至'.$v['date_zone_end'];
           $shopNames             = $shop_names_map[$v['shop_id']]??[];
           $shop_name             = $shopNames['name']??'';
           $list[$k]['shop_name'] = $shop_name;
       }

       $data = [
           'list'                         => $list,
           'total'                        => $res->total(),
       ];
       return  $this->jsonMessage(200,true,'操作成功',$data);
   }


    /**
     * @desc   获取发送消息配置编辑
     * @author luckyXiong
     * @date   2025-01-24
     */
   public function edit($params=[]){
          $id              = $params['id']??'';
          $message         = $params['message']??'';
          $date_zone_start = $params['date_zone_start']??'';
          $date_zone_end   = $params['date_zone_end']??'';
          if($date_zone_end<$date_zone_start){
              return  $this->jsonMessage(201,false,'结束时间必须大于开始时间!',null);
          }

          Db::name('customer_email_config')->where('id',$id)->update(
              [
                  'date_zone_start'=>$date_zone_start,
                  'date_zone_end'  =>$date_zone_end,
                  'message'        =>$message,
              ]
          );
       return  $this->jsonMessage(200,true,'操作成功',null);
   }


    /**
     * @desc   获取发送消息执行接口
     * @author luckyXiong
     * @date   2025-01-24
     */
   public function messageSync($params=[]){
       $id         = $params['id']??'';
       $shop_id    = Db::name('customer_email_config')->where('id',$id)->value('shop_id');
       $operate_id = $params['operate_id']??'';
       $update_id = Db::name('customer_email_log')->insertGetId(
           [
               'source_id'   =>$id,
               'shop_id'     =>$shop_id,
               'operate_id'  =>$operate_id,
               'create_time' =>date("Y-m-d H:i:s", time()),
           ]
       );
       //获取时间区间;
       $info      = Db::name('customer_email_config')->where(['id'=>$id])->find();
       // todo 发送消息数据处理
       $url       = Config('Logisticskey.999_url').'/api/check/send_email';
       $info['update_id'] = $update_id;
       $res       = HttpUtil::postDataCurl($url, $info);
       return  $this->jsonMessage(200,true,'操作成功',null);
   }

    /**
     * @desc   发送消息执行日志接口
     * @author luckyXiong
     * @date   2025-01-24
     */
   public function messageLog($params=[]){
       $id                      = getIsset($params,'id');
       $limit                   = getIsset($params,'limit',10);
       $where = [];
       if($id){
           $where[]  = ['source_id', '=',$id];
       }

       $order = ['create_time desc'];
       $res   = Db::name('customer_email_log')
           ->where($where)
           ->order($order)
           ->paginate($limit);
       $list = $res->items()?:[];
       $shop_ids       = array_column($list,'shop_id');
       $shop_names     = Db::name('shops')->field('id,name,myshopify_domain')->whereIn('id',$shop_ids)->select()->toArray();
       $shop_names_map = array_column($shop_names,null,'id');
       //批量获取创建人;
       $operater_ids    = array_column($list,'operate_id');
       $admin_users     = Db::name('system_admin')->field('id,username')->whereIn('id',$operater_ids)->select()->toArray();
       $admin_users_map = array_column($admin_users,null,'id');
       foreach($list as $k=>$v){
           $shopNames             = $shop_names_map[$v['shop_id']]??[];
           $shop_name             = $shopNames['name']??'';
           $list[$k]['shop_name'] = $shop_name;
           $admin_users_info      = $admin_users_map[$v['operate_id']]??'';
           $list[$k]['operater']  = $admin_users_info['username']??'';
       }

       $data = [
           'list'                         => $list,
           'total'                        => $res->total(),
       ];
       return  $this->jsonMessage(200,true,'操作成功',$data);
   }

    /**
     * @desc   获取操作店铺的列表
     * @author luckyXiong
     * @date   2025-01-24
     */
   public function shopList(){
       $ids  = [17,18,21];
       $data = Db::name('shops')
           ->field('id as shop_id,name as shop_name')
           ->where('status',1)
           ->whereIn('id',$ids)
           ->select()
           ->toarray();
       return  $this->jsonMessage(200,true,'操作成功',$data);
   }












}